<?php
namespace app\api\controller;


use app\common\model\Role as RoleModel;
use app\common\service\EngineerService;
use think\Db;
use util\Tree;

/**
 * 权限管理
 * @package app\index\controller
 */
class MenuRole extends Auth
{
    public $noNeedLogin = [
        //'*'
    ];

    public function get_role_user(){

        if($this->request->isPost()){
            $user_role = Db::name('qt_user_role')->where('status',1)
                ->where('store_id',$this->userInfo['store_id'])
                ->order('id desc, sort desc')
                ->select();
            foreach ($user_role as $key => &$val) {
                $val['user_list'] = Db::name('qt_users')
                    ->where('group',2)
                    ->where('status',0)
                    ->where('post_id',$val['id'])
                    ->where('store_id',$this->userInfo['store_id'])
                    ->field('id as user_id,real_name')
                    ->select();
            }

            $this->result('查询成功',$user_role,200);

        }else {
            $this->result('无效的参数','',204);
        }
    }


    /**
     * 获取所有的菜单
     */
    public function get_all_menu () {

        if($this->request->isPost()){

            $post = $this->request->post();
            $where = [];
            if (isset($post['pid'])) {
                $where['pid'] = $post['pid'];
            }
            $list = Db::name('qt_user_menu')
                ->field('id,title as name,pid,sort,status')
                ->where($where)
                ->order('sort asc')
                ->select();
            $user_role = Tree::toLayer($list);
            $this->result('查询成功',$user_role,200);

        }else {
            $this->result('无效的参数','',204);
        }
    }

    /**
     * 获取用户的菜单权限
     */
    public function get_user_menu(){

        if($this->request->isPost()){
            $post = $this->request->post();
            if (empty($post['user_id'])) {
                $this->result('无效的参数','',204);
            }
            $list_two = Db::name('qt_user_menu_auth')->alias('auth')
                ->field('menu.id,menu.title as name,menu.pid,menu.sort,menu.status')
                ->leftJoin('qt_user_menu menu','auth.menu_id=menu.id')
                ->where('auth.user_id',$post['user_id'])
                ->where('auth.platform',2)
                ->select();
            $user_role_two = Tree::toLayer($list_two);

            $this->result('查询成功',$user_role_two,200);

        }else {
            $this->result('无效的参数','',204);
        }

    }

    /**
     * 添加角色
     *
     * @return mixed|void
     * @throws \think\Exception
     */
    public function add_role()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if(empty($data['name'])){
                $this->result('请填写角色名称','',204);
            }
            $role = RoleModel::where('name',$data['name'])->find();
            if (!empty($role)) {
                $this->result('角色名称不能重复','',204);
            }
            $data['created_time'] = time();
            $data['sort'] = !empty($data['sort']) ? $data['sort'] : 0;
            $data['describe'] = !empty($data['describe']) ? $data['describe'] : 0;
            $data['store_id'] = $this->userInfo['store_id'];
            if (RoleModel::create($data)) {
                $this->result('新增成功','',200);
            } else {
                $this->result('新增失败','',204);
            }
        } else {
            $this->result('无效的参数','',204);
        }
    }


    /**
     * 保存权限
     */
    public function save() {

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (empty($data['type'])) {
                $this->result('类型不能为空','',204);
            }
            if ($data['type'] == 1 && !empty($data['user_id'])) {
                $user_data = Db::name('qt_users')->where('id',$data['user_id'])->find();
                if (empty($user_data)) {
                    $this->result('没有找到用户','',204);
                }

                //插入菜单权限
                EngineerService::updateUserMenuAuth($data['user_id'],explode(',',$data['menu_id']));
            }

            if ($data['type'] == 2 && !empty($data['role_id'])) {
                $role_data = Db::name('qt_user_role')->where('id',$data['role_id'])->find();
                if (empty($role_data)) {
                    $this->result('没有找到角色','',204);
                }
                EngineerService::updateRoleMenu($data['role_id'],explode(',',$data['menu_id']));
            }

            $this->result('操作成功','',200);
        } else {
            $this->result('无效的参数','',204);
        }

    }

    /**
     * 重置权限
     */
    public function reset(){

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if(empty($data['user_id'])){
                $this->result('请填用户ID','',204);
            }
            $user_data = Db::name('qt_users')->where('id',$data['user_id'])->find();
            if (empty($user_data)) {
                $this->result('没有找到用户','',204);
            }

            //插入菜单权限
            EngineerService::insertUserMenuAuth($data['user_id'],$user_data['post_id']);

            $this->result('操作成功','',200);
        } else {
            $this->result('无效的参数','',204);
        }
    }

}
